<template>
    <div id="main">
        <div class="bar">
            <div class="left">
                广告列表
            </div>
            <div class="right">
                <el-button type="success" size="mini"  @click="addMenu"> <i class="el-icon-plus"></i> 添加广告</el-button>
            </div>
        </div>
        <div class="table">
            <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                prop="id"
                label="ID"
                align="center"
              
               >
                </el-table-column>


                <el-table-column
                prop="name"
                label="位置"
                align="center"
  
               >
                </el-table-column>


                <el-table-column
                prop="id"
                label="广告图片"
                align="center"
        
               >
                    <template slot-scope="scope">
                        <img :src="scope.row.picture" alt="" width="100" height="20">
                    </template>
                </el-table-column>
               
               
                <el-table-column
                prop="status"
                align="center"
                label="状态">
                    <template slot-scope="scope">
                        <el-button :type="scope.row.status == 1 ? 'success': 'danger' " size="mini" @click="changeStatus(scope.row.id)">{{ scope.row.status == 1 ? "开启" : "关闭" }}</el-button>
                    </template>
                </el-table-column>

                <el-table-column
               
                align="center"
                label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editMenu(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- dialog -->
        <nav-dialog :DialogData="DialogData" :FormData="FormData" :select="select" @update="getAllNav"></nav-dialog>
    </div>
</template>

<script>
    import NavDialog from "@/components/dialog/NavDialog"
    export default {
        data() {
            return {
                tableData: [],
                DialogData: {
                    title: "",
                    show: false,
                },
                FormData: {},
                select: [],
            }
        },
        components: {
            NavDialog,
        },
        computed: {
            token() {
                return localStorage.token; 
            }
        },
        mounted() {
            this.getAllNav()
            this.getAllxue()
        },
        methods: {
             // 获取所有学历
            async getAllxue() {
                let res = await this.$axios.post("/admin/website/xuelist", { token: this.token })
                // console.log(res);
                let arr = []
                if (res.data.data) {
                    arr = res.data.data
                    arr.forEach(el => {
                        this.select.push({
                            value: el.id,
                            label: el.name
                        })
                    });
                    this.select.unshift({
                        value: 0,
                        label: "首页"
                    })
                }
                // console.log(this.select);
                
            },
            // 获取所有会员
            async getAllNav( id ) {
                let page = id ? id : 0;
                let res = await this.$axios.post("/admin/website/topnavpicshow", { token: this.token , page})
                console.log(res);
                if(res.data.code == 200) {
                    this.tableData = res.data.data
                }
                
            },
            // 编辑菜单
            editMenu( user ) {
                this.DialogData = {
                    show: true,
                    title: "编辑广告",
                    add: false,
                    edit: true
                }
                this.FormData = user
            },
            // 添加菜单
            addMenu() {
                this.DialogData = {
                    show: true,
                    title: "添加广告",
                    add: true,
                    edit: false
                }
                this.FormData = {}
            },
            // 菜单状态改变
            async changeStatus( id ) {
                let res = await this.$axios.post("", {token: this.token, id })
                console.log(res);
                if(res.data.code == 204 || res.data.code == 202) {
                    this.$message.success("更新成功")
                    this.getAllNav()
                } else {
                    this.$message.success("更新失败")
                    this.getAllNav()
                }
                
            },
        },
    }
</script>

<style lang="scss" scoped>
 #main {
     width: 100%;
     box-sizing: border-box;
     .bar {
         width: 100%;
         font-weight: bold;
         display: flex;
         justify-content: space-between;
         height: 40px;
         line-height: 40px;
         border-bottom: 1px solid #aaa;
     }
     .table {
         margin-top: 20px;
     }
     .pic {
         width: 40px;
         height: 40px;
         display: block;
     }
 }
</style>